package com.gkzf.ai.module.crm.util.assembly;

import com.alibaba.fastjson.JSONObject;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.admittancecondition.OverdueRecordVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static com.gkzf.ai.module.crm.util.AnalysisJsonObjectUtils.COMMA;

/**
 * 逾期信息汇总
 */
public class OverdueRecordSummary {

    private static final String  OVERDUE_INFO_SUMMARY = "逾期信息汇总";

    public static final String CURRENT_OVERDUE = "当前逾期";

    public static final String NEARLY_HALF_YEAR = "近半年";

    public static final String NEARLY_ONE_YEAR = "近一年";

    public static final String NEARLY_TOW_YEAR = "近两年";

    public static final String NEARLY_FIVE_YEAR = "近五年";

    public static final String OVERDUE_NUMBER = "逾期次数";

    public static final String MAX_CONTINUITIES_NUMBER = "最大连续次数";

    public static final String MAX_OVERDUE_MONTH = "最长逾期月数";

    public static final String MAX_OVERDUE_MONEY = "最高逾期金额";



    /**
     *  贷款 获取逾期明细
     * @param param 数据对象
     * @param resultMap 存储信用卡集合的map
     * @return
     */
    public void getOverdueRecordLoansVoList(Map<String, Object> param, Map<String,List<OverdueRecordVo>> resultMap,String overdueType,List<OverdueRecordVo> summaryList){
        Object overdueSummaryList = param.get(OVERDUE_INFO_SUMMARY);
        List<OverdueRecordVo> overdueRecordVos = new ArrayList<>();
        if (overdueSummaryList instanceof JSONObject overdueSummary) {
            AtomicInteger atomicInteger = new AtomicInteger(0);
            for (Map.Entry<String, Object> summary : overdueSummary.entrySet()) {
                switch (summary.getKey()) {
                    case CURRENT_OVERDUE:
                        atomicInteger.set(1);
                        overdueRecordVos.add(getCreditReportCreditOverdueDO((JSONObject) summary.getValue(), atomicInteger));
                        break;
                    case NEARLY_HALF_YEAR:
                        atomicInteger.set(6);
                        overdueRecordVos.add(getCreditReportCreditOverdueDO((JSONObject)summary.getValue(), atomicInteger));
                        break;
                    case NEARLY_ONE_YEAR:
                        atomicInteger.set(12);
                        overdueRecordVos.add(getCreditReportCreditOverdueDO((JSONObject)summary.getValue(), atomicInteger));
                        break;
                    case NEARLY_TOW_YEAR:
                        atomicInteger.set(24);
                        overdueRecordVos.add(getCreditReportCreditOverdueDO((JSONObject)summary.getValue(), atomicInteger));
                        break;
                    case NEARLY_FIVE_YEAR:
                        atomicInteger.set(60);
                        overdueRecordVos.add(getCreditReportCreditOverdueDO((JSONObject)summary.getValue(), atomicInteger));
                        break;
                }
            }
        }
        if (!overdueRecordVos.isEmpty()){
            summaryList.addAll(overdueRecordVos);
            resultMap.put(overdueType,overdueRecordVos);
        }
    }


    private static OverdueRecordVo getCreditReportCreditOverdueDO(JSONObject jsonObject,AtomicInteger atomicInteger){
        OverdueRecordVo overdueRecordVo = new OverdueRecordVo();
        // 从 map 中获取值并设置到 OverdueRecordVo 对象中
        // 这一系列 if 检查并设置属性是公共的，但它们都是在这个 "当前逾期" 的 case 中执行的
        overdueRecordVo.setOverUeRecordMonths(atomicInteger+COMMA+AssembleUtils.getStringValue(jsonObject,OVERDUE_NUMBER));
        overdueRecordVo.setContinuousOverdueMonths(atomicInteger+COMMA+AssembleUtils.getStringValue(jsonObject,MAX_CONTINUITIES_NUMBER));
        overdueRecordVo.setMaxMonthsOverdueMonths(atomicInteger+COMMA+AssembleUtils.getStringValue(jsonObject,MAX_OVERDUE_MONTH));
        overdueRecordVo.setMaxMonthsOverdueAmountMonths(atomicInteger+COMMA+AssembleUtils.getStringValue(jsonObject,MAX_OVERDUE_MONEY));
        return overdueRecordVo;
    }
}
